#pragma once

#include <iostream>
#include <memory>
#include "Protocol.hpp"
namespace CalCulate
{
    enum
    {
        Success = 0,
        DivZeroErr,
        ModZeroErr,
        UnKnowOper
    };
    class Calculate
    {
    public:
        Calculate()
        {
        }
        std::shared_ptr<Protocol::Response> Cal(std::shared_ptr<Protocol::Request> req)
        {
            std::shared_ptr<Protocol::Response> resp = factory.BuildResponse();
            resp->SetCode(Success);
            switch (req->GetOper())
            {
            case '+':
                resp->SetResult(req->Getx() + req->Gety());
                break;
            case '-':
                resp->SetResult(req->Getx() - req->Gety());
                break;
            case '*':
                resp->SetResult(req->Getx() * req->Gety());
                break;
            case '/':
                if (req->Gety() == 0)
                {
                    resp->SetCode(ModZeroErr);
                }
                else
                {

                    resp->SetResult(req->Getx() / req->Gety());
                }
                break;

            case '%':
                if (req->Gety() == 0)
                {
                    resp->SetCode(DivZeroErr);
                }
                else
                {

                    resp->SetResult(req->Getx() % req->Gety());
                }
                break;

            default:
                resp->SetCode(UnKnowOper);
                break;
            }
            return resp;
        }
        ~Calculate()
        {
        }

    private:
        Protocol::Factory factory;
    };
}